package com.xwq.thumb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xwq.thumb.constant.UserConstant;
import com.xwq.thumb.exception.BusinessException;
import com.xwq.thumb.exception.ErrorCode;
import com.xwq.thumb.model.entity.User;
import com.xwq.thumb.service.UserService;
import com.xwq.thumb.mapper.UserMapper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

/**
* @author XWQ
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-04-17 20:10:22
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{


    /**
     * 获取当前登录用户
     * @param request http亲求
     * @return 登录的用户对象
     */
    @Override
    public User getLoginUser(HttpServletRequest request) {
        // 先判断是否已登录
        Object userObj = request.getSession().getAttribute(UserConstant.LOGIN_USER);
        User currentUser = (User) userObj;
        if (currentUser == null || currentUser.getId() == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        // 从数据库查询（追求性能的话可以注释，直接返回上述结果）
        long userId = currentUser.getId();
        currentUser = this.getById(userId);
        if (currentUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        return currentUser;
    }

}




